/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler;

import com.gibbon.jme.context.JmeContext;
import com.gibbon.jme.context.lwjgl.LWJGLCanvas;

import javax.swing.*;
import java.awt.*;

public class JMEFrame extends JFrame {

    private JmeContext context;
    protected LWJGLCanvas canvas;

    public JMEFrame( String name, int width, int height) {
        super( name );
        setPreferredSize( new Dimension( width, height ) );
        setName( name );
        setVisible( true );
        setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        initComponents();
        validateContextSystem();

    }

    public JmeContext getContext(){
        return context;
    }

    public Canvas getCanvas() {
        return context.getCanvas();
    }

    private void initComponents() {
        canvas = createCanvas();
        canvas.setBounds(0, 0, getWidth(), getHeight());

        getContentPane().setLayout( new java.awt.BorderLayout() );
        getContentPane().add(canvas, java.awt.BorderLayout.CENTER);
        pack();
    }

    protected LWJGLCanvas createCanvas(){
        try {
            if (JmeContext.get()!=null){
                // share with the previous context
                context = JmeContext.create(JmeContext.getDefaultImplementorClassName(),
                                            JmeContext.CONTEXT_CANVAS,
                                            JmeContext.get());
            }else{
                context = JmeContext.create(JmeContext.getDefaultImplementorClassName(),
                                            JmeContext.CONTEXT_CANVAS);
            }
        } catch (InstantiationException ex) {
            ex.printStackTrace();
            return null;
        }

        context.start();
        canvas = (LWJGLCanvas) context.getCanvas();
        return canvas;
    }

    public void validateContextSystem() {

        JmeContext test_context = JmeContext.get();
        try {
            // makes sure DisplaySystem.getDisplaySystem().getRenderer() is not null
            test_context.waitFor();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
